//******************************************************************/
//版本说明:
//V0.1		2017-03-30	11:00	yshao	复制自SL909_G01_X32
//V2.1		2019-02-07	11:00	yshao	复制自LL901_G04_X02_4
//******************************************************************/
//******************************************************************/
//			   全局参数定义
//******************************************************************/
//仿真模式
//`define	SIMULATION	1
//`timescale	1ps/1ps

//FPGA程序模式
//`define	APP_MODE	1
`define		TEST_MODE	2
//`define	BOOT_MODE	3

//程序功能模式
`define		SUB		0
//`define	ASSIST		1

//******************************************************************/
//			   FPGA顶层
//******************************************************************/
module SL909_P04(
                input	wire		sclkin,
                input	wire		key_in,
                
                output  wire            JP202_PIN3,
                output  wire            JP202_PIN4,
		output	wire		JP202_PIN5,
                input	wire            JP202_PIN6,

		output	wire		sa_clk,
		output	wire	[2:0]	sa_cnt,
		output	wire		sa_dqm_l,
		output	wire		sa_dqm_h,
		output	wire	[10:0]	sa_addr,
                output	wire	[1:0]	sa_bank,
		inout	tri	[31:0]	sa_data,

		input	wire		mcu_dmx_tx,		//uart2_sync,
		output	wire		mcu_dmx_rx,		//uart3_sync,	

		input	wire		mcu_fpga_ctrl,		//mcu到fpga的模式控制信号

		input	wire		mcu_spi_fpga,		//spi工作在通讯模式		
		input	wire		mcu_spi_clk,
		input	wire		mcu_spi_mosi,
		output	wire		mcu_spi_miso,

		output	wire		spi2_cs,
		output	wire		spi2_clk,
		output	wire		spi2_mosi,
								
		output	wire		sd_clk,
		inout	tri		sd_cmd_sdi,
		input	wire		sd_dat0_sdo,
		input	wire		sd_dat1,
		input	wire		sd_dat2,
		inout	wire		sd_dat3_cs,
		input	wire		sd_cd,
		input	wire		sd_wp,

		input	wire		gp0_rxc,
		input	wire		gp0_rxdv,
		input	wire	[3:0]	gp0_rxd,
		output	wire		gp0_txc,
		output	wire		gp0_txen,
		output	wire	[3:0]	gp0_txd,
		
		input	wire		gp1_rxc,
		input	wire		gp1_rxdv,
		input	wire	[3:0]	gp1_rxd,
		output	wire		gp1_txc,
		output	wire		gp1_txen,
		output	wire	[3:0]	gp1_txd,

	        //Hub接口
	        output	wire	[11:0]	left_hub,
	        input	wire	[11:0]	right_hub,         
		
		input	wire	[7:0]	port_in,
		
		//LED指示灯
		output	wire		led_g
		);

//****************************************************************
//		内部信号
//****************************************************************
wire		resetb, oclk, sclk, clk_25M;
wire	[20:0]	time_us, sync_us;

//MCU设置总线相关信号
wire		mcu_set_d_ok, mcu_ext_d_ok;
wire	[31:0]	mcu_set_addr;
wire	[7:0]	mcu_set_data;
reg	[7:0]	mcu_rd_data;

//错误指示
wire 		crc32_err0, crc32_err1, phy_no_data_index0, phy_no_data_index1;
wire 		unmatch1 ,unmatch2, unmatch3;   
wire	[3:0]	sa_dqm_top;
wire	[11:0]	hub_test_result;
wire	[7:0]	phy_result;
wire	[15:0]	hub_result;
wire	[7:0]	sdram_result;

//**********调试用信号**********
wire	[31:0]	a1_tout;

//******************************************************************/
//			   参数定义
//******************************************************************/
//Flash程序区地址
parameter	INIT_ADDR	=14'b0000_0000_1100_00;//0x00C0_0000~0x00C3_FFFF
parameter	TEST_ADDR	=14'b0000_0000_1100_01;//0x00C4_0000~0x00C7_FFFF
parameter	ACT_ADDR	=14'b0000_0000_1100_10;//0x00C8_0000~0x00CB_FFFF

//程序版本信息
parameter	MAIN_FUNCTION	=  "S";		//ASCII "S"  
parameter	SUB_FUNCTION	=  "L";		//ASCII "L"  
parameter	MAIN_SOLUTION	=  9;		//"9"        
parameter	SUB_SOLUTION	=  9;		//"09"       
parameter	APPLICATION_TYPE=  "P";		//ASCII "G"  
parameter	MAIN_VERSION	=  8'd4;	//"03"       
parameter	SUB_VERSION	=  8'd2;	//"X01"  
parameter	MINI_VERSION	=  8'd2;	//" "  

//模块参数设置
defparam	comm_top.phy_comm.com_ctrl.INITIAL_ADDR		=INIT_ADDR;
defparam	comm_top.phy_comm.com_ctrl.TEST_ADDR		=TEST_ADDR;
defparam	comm_top.phy_comm.com_ctrl.ACTIVE_ADDR		=ACT_ADDR;

defparam	comm_top.phy_comm.state_ctrl.main_function	=MAIN_FUNCTION;
defparam	comm_top.phy_comm.state_ctrl.sub_function	=SUB_FUNCTION;
defparam	comm_top.phy_comm.state_ctrl.main_solution	=MAIN_SOLUTION;
defparam	comm_top.phy_comm.state_ctrl.sub_solution	=SUB_SOLUTION;
defparam	comm_top.phy_comm.state_ctrl.application_type	=APPLICATION_TYPE;
defparam	comm_top.phy_comm.state_ctrl.main_version	=MAIN_VERSION;
defparam	comm_top.phy_comm.state_ctrl.sub_version	=SUB_VERSION;
defparam	comm_top.phy_comm.state_ctrl.mini_version	=MINI_VERSION;

defparam        comm_top.mcu_comm.main_function		=	MAIN_FUNCTION;
defparam        comm_top.mcu_comm.sub_function		=	SUB_FUNCTION;
defparam        comm_top.mcu_comm.main_solution		=	MAIN_SOLUTION;
defparam        comm_top.mcu_comm.sub_solution		=	SUB_SOLUTION;
defparam        comm_top.mcu_comm.application_type	=	APPLICATION_TYPE;
defparam        comm_top.mcu_comm.main_version		=	MAIN_VERSION;
defparam        comm_top.mcu_comm.sub_version		=	SUB_VERSION;
defparam        comm_top.mcu_comm.mini_version		=	MINI_VERSION;

//**************************************************************
//			通讯模块
//**************************************************************
a1_comm_top comm_top(
		//时钟，按键，Led
		.sclkin(sclkin),
		.key_in(key_in),
		.led_g(led_g),
        
		//输出复位和时钟
		.resetb(resetb),
		.sclk(sclk),
		.oclk(oclk),
		.sa_clk(sa_clk),
		.clk_25M(clk_25M),

                //MCU通讯接口
		.mcu_fpga_ctrl(mcu_fpga_ctrl),
		
		.mcu_spi_fpga(mcu_spi_fpga),
		.mcu_spi_clk(mcu_spi_clk),
		.mcu_spi_mosi(mcu_spi_mosi),
		.mcu_spi_miso(mcu_spi_miso),
        
		.spi2_cs(spi2_cs),
		.spi2_clk(spi2_clk),
		.spi2_mosi(spi2_mosi),
		
		//PHY通讯接口
		.gp0_rxc(0),
		.gp0_rxdv(0),
		.gp0_rxd(0),
		.gp0_txc(),
		.gp0_txen(),
		.gp0_txd(),
		
		.gp1_rxc(0),
		.gp1_rxdv(0),
		.gp1_rxd(0),
		.gp1_txc(),
		.gp1_txen(),
		.gp1_txd(),
        
		//SD卡通讯接口
		.sd_clk(sd_clk),
		.sd_cmd_sdi(sd_cmd_sdi),
		.sd_dat0_sdo(sd_dat0_sdo),
		.sd_dat1(sd_dat1),
		.sd_dat2(sd_dat2),
		.sd_dat3_cs(sd_dat3_cs),
		.sd_cd(sd_cd),
		.sd_wp(sd_wp),
		
		//分频时钟
		.time_us(time_us),
		.sync_us(sync_us),

		//MCU总线接口
		.mcu_set_d_ok(mcu_set_d_ok),
		.mcu_ext_d_ok(mcu_ext_d_ok),
		.mcu_set_addr(mcu_set_addr),
		.mcu_set_data(mcu_set_data),
		.mcu_rd_data(mcu_rd_data),
	
		//PHY总线接口
		.init_mode(),
		.set_d_ok(),
		.ext_d_ok(),
		.set_addr(),
		.set_data(),
		.rd_data(0),
	
		//显示数据接口
		.vs(),
		.ds(),
		.data(),
		.h_start(),
		.h_num(),
		.l2048_mode(),
		.artnet_mode(),
		
		.state(),
		.color_restore_type(),
		.led_light(),
		.unit_color_adj(),
		.testmode(),
		
		.black(),
		.identify_flag(),
		
		//背板控制接口
		.fpga_dmx_send(0),
		.mcu_dmx_ten(),
		
		//SD卡播放指示
		.input_active(),
		.sd_valid(),
	        .sd_play_flag(0),
	        
		//SD卡播放操作接口
		.sd_play_clk(0),
		.sd_play_cmd_out(0),
		.sd_play_cmd_oe(0),
		.sd_play_data(),
		//SD发送数据包接口
		.dis_send_flag(0),
		.dis_pre_flag(0),
		.dis_send_data(0),
		
		//调试接口
		.tout(a1_tout)   
		);
		
//**************************************************************
//		测试模块
//**************************************************************		                           
//phy测试
phy_test_top  phy_test_top(
		.sclk(sclk),
                .tclk(sclk),//tclk),
                .resetb(resetb),
                .time_1ms_sync(sync_us[10]),
                           
                .gp0_rxc(gp0_rxc),
		.gp0_rxdv(gp0_rxdv),
		.gp0_rxd(gp0_rxd),
		.gp0_txc(gp0_txc),
		.gp0_txen(gp0_txen),
		.gp0_txd(gp0_txd),
		                
               	.crc32_err0(crc32_err0),
                           
                .gp1_rxc(gp1_rxc),
		.gp1_rxdv(gp1_rxdv),
		.gp1_rxd(gp1_rxd),
		.gp1_txc(gp1_txc),
		.gp1_txen(gp1_txen),
		.gp1_txd(gp1_txd),
		                
               	.crc32_err1(crc32_err1),
               	.phy_no_data_index0(phy_no_data_index0),
               	.phy_no_data_index1(phy_no_data_index1)
		);		                           
	
//sdram测试
sdram_test_top  sdram_test_top(
		.resetb(resetb),
		.sclk(sclk),

		.sa_clk(),
		.sa_cnt(sa_cnt),
		.sa_dqm(sa_dqm_top),
		.sa_addr(sa_addr),
   		.sa_bank(sa_bank),
		.sa_data(sa_data),

		.tout(tout),
		.unmatch1(unmatch1),
		.unmatch2(unmatch2),
		.unmatch3(unmatch3)
		);
											
assign	sa_dqm_l=sa_dqm_top[0];
assign	sa_dqm_h=sa_dqm_top[2];

//Hub测试
hub_test_data hub_test_data(
		.sclk(sclk),
		.resetb(resetb),
		.time_1ms_sync(sync_us[10]),
		.left_hub(left_hub),
		.right_hub(right_hub),
		.hub_test_result(hub_test_result)
		);  

//检测结果汇总
ctrl_dsel ctrl_dsel(
		.sclk(sclk),
		.resetb(resetb),
		
		.crc32_err0(crc32_err0),
		.crc32_err1(crc32_err1),
		.phy_no_data_index0(phy_no_data_index0),
		.phy_no_data_index1(phy_no_data_index1),
		.unmatch1(unmatch1),
		.unmatch2(unmatch2),
		.unmatch3(unmatch3),
		.hub_test_result(hub_test_result),
                
		.phy_result(phy_result),
		.hub_result(hub_result),
		.sdram_result(sdram_result),
		
		.led_flash_en(led_flash_en)
		);

always @(posedge sclk)
	case (mcu_set_addr)
		32'h00011000:	mcu_rd_data <= phy_result;
		32'h00011001:	mcu_rd_data <= sdram_result;
		32'h00011002:	mcu_rd_data <= hub_result[7:0];
		32'h00011003:	mcu_rd_data <= hub_result[15:8];
		default:	mcu_rd_data <= 0;
	endcase

//************************************************************/
//		调试接口
//************************************************************/                    
//assign  port_le = {sd_clk,sd_cmd_sdi,sd_dat0_sdo,sd_dat1,sd_dat2,sd_dat3_cs,f_start,f_work};  
//assign  port_le = {sd_clk,sd_cmd_sdi,sd_dat0_sdo,sd_dat1,sd_dat2,sd_tout[3],sd_tout[2],sd_tout[1]};  
//assign  port_le = {sd_clk,sd_cmd_sdi,sd_dat0_sdo,sd_dat1,sd_dat2,sd_dat3_cs,dis_send_flag,dis_pre_flag};  
//assign  port_da = a1_tout;  
//assign  port_le = a1_tout;
//assign  port_le = {mcu_spi_fpga, mcu_spi_clk, mcu_spi_mosi, mcu_spi_miso, mcu_fpga_ctrl, mcu_sd_en, mcu_sd_rd};  
//assign  port_da = spi_tout;

//assign	JP202_PIN3 = mcu_spi_fpga;//sys_tout[1];//spi2_cs & mcu_spi_fpga;
//assign	JP202_PIN4 = time_us[15];//ttt_count[9];//spi2_clk | mcu_spi_clk;
//assign	JP202_PIN5 = a1_tout[0];//spi2_mosi | mcu_spi_mosi;

assign	JP202_PIN3 = mcu_spi_fpga;//sys_tout[1];//spi2_cs & mcu_spi_fpga;
assign	JP202_PIN4 = time_us[16];//mcu_set_d_ok;//ttt_count[9];//spi2_clk | mcu_spi_clk;
assign	JP202_PIN5 = a1_tout[0];//mcu_ext_d_ok;//spi2_mosi | mcu_spi_mosi;

endmodule
